//
//  KoiAccountingApp.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 16.03.25.
//

import SwiftUI
import Toasts

@main
struct KoiAccountingApp: App {

    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    @StateObject var appState = AppState()
    @StateObject var bookSelection = BookSelection()
    
    init() {
        #if DEBUG
            print("debug mode")
        #elseif DEBUGLOCAL
            print("debug local mode")
        #endif
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .installToast(position: .top)
                .environmentObject(bookSelection)
                .environmentObject(appState)
                .onOpenURL { url in
                    WXApi.handleOpen(url, delegate: WeChatAuthManager.shared)
                }
        }
    }
}

//class AppDelegate : NSObject, UIApplicationDelegate {
//    
//    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
//        
//        UMCommonLogSwift.setUpUMCommonLogManager()
//        UMCommonSwift.setLogEnabled(bFlag: true)
//        UMCommonSwift.initWithAppkey(appKey: "67f784bc65c707471a453875", channel: "AppStore")
//        UMSocialManager.default()?.setPlaform(.wechatSession, appKey: "", appSecret: "", redirectURL: nil)
//        return true
//    }
//    
//    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
//        return UMSocialManager.default()?.handleOpen(url, options: options) ?? false
//    }
//}

import UIKit
import UMCommon

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?  // 关键：添加 window 属性
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if PrivacyGuide.standard.isAgree() {
            // 可在此初始化第三方库或其他配置
            UMCommonLogSwift.setUpUMCommonLogManager()
            // UMCommonSwift.setLogEnabled(bFlag: true)
            UMCommonSwift.initWithAppkey(appKey: "67f784bc65c707471a453875", channel: "AppStore")
            // 友盟分享没有用，收不到回调
            // UMSocialManager.default()?.setPlaform(.wechatSession, appKey: "wx0f1e11bb110abf9d", appSecret: "719721c8ba04be30c4986ed035d14c86", redirectURL: nil)
        }
        
        WXApi.registerApp("wx0f1e11bb110abf9d", universalLink: "https://help.koiaccounting.com/KoiAccounting/")
        
        return true
    }
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        sceneConfig.delegateClass = SceneDelegate.self
        return sceneConfig
    }
    
//    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
//        return UMSocialManager.default()?.handleOpen(url, options: options) ?? false
//    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        print("回调 open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]")
        return WXApi.handleOpen(url, delegate: WeChatAuthManager.shared)
    }

    // 友盟分享/登录回调
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        print("回调 open url: URL, sourceApplication: String?, annotation: Any")
        return WXApi.handleOpen(url, delegate: WeChatAuthManager.shared)
        // return UMSocialManager.default().handleOpen(url)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([any UIUserActivityRestoring]?) -> Void) -> Bool {
        print("回调 application  continue userActivity: NSUserActivity, restorationHandler")
        return WXApi.handleOpenUniversalLink(userActivity, delegate: WeChatAuthManager.shared)
        // return UMSocialManager.default().handleUniversalLink(userActivity)
    }

}


class SceneDelegate : NSObject, UIWindowSceneDelegate {
    
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        print("回调 scene(_ scene: UIScene, continue userActivity: NSUserActivity)")
        WXApi.handleOpenUniversalLink(userActivity, delegate: WeChatAuthManager.shared)
    }
}
